/* This test just loads an image through the ekeko library, so it will the test
 * the creation of contexts and image loading, both sync and async
 */

#include <Ekeko.h>

static void _help(void)
{
	printf("test01 IMAGE_TO_LOAD\n");
}

static void _image_loaded(Enesim_Surface *s, Eix_Error *err, void *user_data)
{
	printf("T01: Image loaded async in %p\n", s);
	/* can we send another message here? */
	if (s) enesim_surface_unref(s);
}

int main(int argc, char **argv)
{
	Ekeko_Context *ctx;
	Enesim_Surface *s = NULL;

	if (argc <= 1)
	{
		_help();
		return -1;
	}

	ekeko_init();
	enesim_image_init();

	if (!ekeko_is_connected())
	{
		printf("failed to connect\n");
		return -1;
	}
	/* create the context */
	ctx = ekeko_context_new("test01", 8192 * 1024);
	if (!ctx)
	{
		printf("failed to create the context\n");
		return -1;
	}

	printf("context of name %s and size %d\n", ekeko_context_name_get(ctx),
			ekeko_context_size_get(ctx));
	/* now load the image */
	printf("T01: Loading sync image '%s'\n", argv[1]);
	ekeko_image_load(ctx, argv[1], &s, NULL);
	if (!s)
	{
		printf("failed to load the image\n");
		return -1;
	}
	printf("T01: Loading async image '%s'\n", argv[1]);
	ekeko_image_load_async(ctx, argv[1], _image_loaded, NULL);
	ekeko_sync();

	enesim_image_file_save("test01.png", s, NULL);
	enesim_surface_unref(s);
	ekeko_context_free(ctx);

	enesim_image_shutdown();
	ekeko_shutdown();

	return 0;
}
